////此页面为物流管理部分公用的函数

$(document).ready(function() {
	/////订单状态查询
	//	输入:必须在页面中设定 search_state
	initSearchData();

})
var SEARCH_CUSTOMER_TYPE = 1; //用户组
var SEARCH_CUSTOMER_NAME = 2; //配送单位
var SEARCH_DISTRIBUTION_DATE_START = 4; //配送开始时间
var SEARCH_DISTRIBUTION_DATE_END = 8; //配送结束时间
var SEARCH_STATE = 16; //订单状态
var SEARCH_STOREHOUSE_NAME = 32; //库房
var SEARCH_INVENTORY_MAN = 64; //盘存人
var SEARCH_INVENTORY_DATE_START = 128; //盘存开始时间
var SEARCH_INVENTORY_DATE_END = 256; //盘存结束时间
var SEARCH_PURCHASE_MAN = 512; //采购员
var SEARCH_PURCHASE_DATE_START = 1024;//查询采购时间
var SEARCH_PURCHASE_DATE_END = 2048;//查询采购时间
var SEARCH_STOREHOUSE_ID = 4096; //库房(ID)
var SEARCH_CREATE_MAN = 8192;//建单人
var SEARCH_ISVERIFY=16384;
var SEARCH_ORDER_NO=32768;//查询订单编号
var SEARCH_DELIVERY_METHOD=65536;//查询配送方式
var SEARCH_TRADE_TYPE=131072;

function buildSearchOptions(searchType, newdata) {
	var data = [{
		"name": "用户组",
		"type": "CUSTOMER_TYPE",
		"className": "easyui-combobox",
		"id": "search_customer_type"
	}, {
		"name": "配送单位",
		"type": "CUSTOMER_NAME",
		"className": "easyui-combobox",
		"id": "search_customr_name"
	}, {
		"name": "配送开始时间",
		"type": "DISTRIBUTION_DATE_START",
		"className": "easyui-datebox",
		"id": "search_begin_time"
	}, {
		"name": "配送结束时间",
		"type": "DISTRIBUTION_DATE_END",
		"className": "easyui-datebox",
		"id": "search_end_time"
	}, {
		"name": "订单状态",
		"type": "STATE",
		"className": "easyui-combobox",
		"id": "search_state"
	}, {
		"name": "库房",
		"type": "STOREHOUSE_NAME", //库房
		"className": "easyui-combobox",
		"id": "search_storehouse"
	}, {
		"name": "盘存人",
		"type": "INVENTORY_MAN", //盘存人
		"className": "easyui-combobox",
		"id": "search_man"
	}, {
		"name": "盘存开始时间",
		"type": "INVENTORY_DATE_START", //盘存开始时间
		"className": "easyui-datebox",
		"id": "search_begin_time"
	}, {
		"name": "盘存结束时间",
		"type": "INVENTORY_DATE_END",
		"className": "easyui-datebox",
		"id": "search_end_time"
	}, {
		"name": "采购员",
		"type": "PURCHASE_MAN", //采购员
		"className": "easyui-combobox",
		"id": "search_purchaser"
	}, {
		"name": "采购开始时间",
		"type": "PURCHASE_DATE_START",
		"className": "easyui-datebox",
		"id": "search_purchase_date_start"
	},
	{
		"name": "采购结束时间",
		"type": "PURCHASE_DATE_END",
		"className": "easyui-datebox",
		"id": "search_purchase_date_end"
	},{
		"name": "库房",
		"type": "STOREHOUSE_ID",
		"className": "easyui-textbox",
		"id": "search_storehouse_id"
	}, {
		"name": "建单人",
		"type": "CREATE_MAN",
		"className": "easyui-combobox",
		"id": "search_create_man"
	},
	{
		"name": "审核状态",
		"type": "ISVERIFY",
		"className": "easyui-combobox",
		"id": "input_ISVERIFY"
	},
	{
		"name": "编号",
		"type": "ORDER_NO",
		"className": "easyui-textbox",
		"id": "input_ORDER_NO"
	},
	{
		"name": "配送方式",
		"type": "DELIVERY_METHOD",
		"className": "easyui-combobox",
		"id": "input_DELIVERY_METHOD"
	},
	{
		"name": "类型名称",
		"type": "TRADE_TYPE",
		"className": "easyui-textbox",
		"id": "input_TRADE_TYPE"
	},];
	var content = "";
	var s;
	for(var i = 0; i < data.length; i++) {
		s = searchType & (1 << i);
		if(s != 0) {
			content += "<td>" + data[i].name + "</td><td><input class=\"" + data[i].className + "\" name=\"" + data[i].type + "\" id=\"" + data[i].id + "\"/></td>";
		}
	}
	if(newdata.length > 0) {
		for(var i = 0; i < newdata.length; i++) {
			content += "<td>" + newdata[i].name + "</td><td><input class=\"" + newdata[i].className + "\" name=\"" + newdata[i].type + "\" id=\"" + newdata[i].id + "\"/></td>";
		}
	}
	var contain = "<div id=\"p\" class=\"easyui-panel\" title=\"查询\" style=\"width:100%;height:100%;\"><table cellspacing=\"4\" style=\"margin-top: 10px;\"><tr>" + content;
	contain += "<td><a href=\"#\" class=\"easyui-linkbutton\" onclick=\"doSearch()\" data-options=\"iconCls:'icon-search'\" style=\"width:80px;\">查询</a></td></tr></table></div>";
	return contain;
}

function initSearchData() {
	if($('#search_man') != undefined){
		$('#search_man').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url:RemoteLinkAddress + "/user/listall.do",
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/user/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
		});
	}
	if($('#search_create_man') != undefined){
		$('#search_create_man').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url:RemoteLinkAddress + "/user/listall.do",
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/user/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
		});
	}
	
	if($("#search_storehouse_id") != undefined) {
		$('#search_storehouse_id').combobox({
			valueField: 'ID',
			textField: 'NAME',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			editable:false,
			url: RemoteLinkAddress + "/logistics/storehouse/listall.do",
		});
	}
	if($("#search_storehouse") != undefined) {
		$('#search_storehouse').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			editable:false,
			url: RemoteLinkAddress + "/logistics/storehouse/listall.do",
		});
	}
	if($("#search_customer_type") != undefined) {
		$('#search_customer_type').combobox({
			valueField: 'CUSTOMER_TYPE',
			textField: 'CUSTOMER_TYPE',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			editable:false,
			url: RemoteLinkAddress + "/logistics/customertype/listall.do",
			onSelect:function(record)
			{
				var _url="";
				_url=RemoteLinkAddress + '/logistics/customer/listall.do?CUSTOMER_TYPE='+record.CUSTOMER_TYPE;
				$('#search_customr_name').combobox("reload",_url);
			}
		});
	}
	if($("#search_customr_name") != undefined) {
		$('#search_customr_name').combobox({
			valueField: 'CUSTOMER_NAME',
			textField: 'CUSTOMER_NAME',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/logistics/customer/listall.do',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/logistics/customer/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			}
		});
	}
	if($("#search_state") != undefined) {
		var state = [];
		$.ajax({
			type: "get",
			url: "",
			dataType: 'JSON',
			url: RemoteLinkAddress + '/logistics/originalorder/getstate.do',
			success: function(result) {
				var i = 0;
				for(var i = 0; i < result.length; i++) {
					var s = new Object();
					s.id = result[i];
					s.text = result[i];
					state[i] = s;
				}
				$("#search_state").combobox({
					data: state,
					valueField: 'id',
					textField: 'text'
				});
			}
		});
	}
	if($("#search_purchaser") != undefined) {
		$("#search_purchaser").combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/user/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
		});
	}
	if($("#input_ISVERIFY") != undefined) {
		$("#input_ISVERIFY").combobox({
			panelHeight: '50px',
			valueField: 'value',
			textField: 'text',
			data: [{
				value: '0',
				text: '未审核'
			}, {
				value: '1',
				text: '已审核'
			}]
		});
	}
	
	$('#input_DELIVERY_METHOD').combobox({
		valueField: 'DELIVERY_METHOD',
		textField: 'DELIVERY_METHOD',
		panelHeight: 'auto',
		mode: 'remote',
		method: 'get',
		editable:false,
		url: RemoteLinkAddress + '/logistics/delivertype/listall.do',
	});
	if($("#search_customer_type") == undefined || $("#search_customr_name") == undefined || $("#search_state")) {
		console.log("请检查查询项的用户组、配送单位和订单状态是否存在或者ID命名是否规范");
	}
}

function processSearchCondition() {
	if($("#div_search") == undefined) {
		console.log("请检查ID为div_search是否存在");
		return "";
	}
	var searchCondition = "";
	$("#div_search input[name]").each(function(index) {
		if($(this).val().trim().length != 0) {
			if(searchCondition == "") {
				searchCondition = $(this).attr("name") + "=" + $(this).val().trim();
			} else {
				searchCondition += "&" + $(this).attr("name") + "=" + $(this).val().trim();
			}
		}
	});
	return searchCondition;
}